<?php
/*
Plugin Name: SEIT Class Management
Plugin URI: http://nangvang.vn/develop/plugins/SeitCM/
Description: Sunshine class management
Author: Shunshine Developer
Version: 0.01
Author URI: http://nangvang.vn/develop/
*/
require_once 'seitcm_config.php';
require_once SEITCM_INCLUDES_DIR.'seitcm_menu.php';
add_action( 'admin_menu', 'seitcm_menu' );
/*
 * Register hook for activate/inactivate/uninstall SEITCM Plugin
 * */
register_activation_hook(__FILE__, 'seitcm_activate_me');
register_deactivation_hook(__FILE__, 'seitcm_deactivate_me');
register_uninstall_hook(__FILE__, 'seitcm_uninstall_me');

/*
 * activate SEITCM Plugin
 * */
function seitcm_activate_me() {
	if(current_user_can('activate_plugins'))
	{
		require_once SEITCM_INCLUDES_DIR.'seitcm_dbinit.php';
		seitcm_init_database();
		require_once SEITCM_INCLUDES_DIR.'seitcm_custom_roles.php';
		seitcm_addrole();
	}
}

/*
 * Deactivate SEITCM Plugin
 * */
function seitcm_deactivate_me() {
	if(current_user_can('activate_plugins'))
	{
		require_once SEITCM_INCLUDES_DIR.'seitcm_custom_roles.php';
		seitcm_remove_role();
		//require_once SEITCM_INCLUDES_DIR.'seitcm_dbinit.php';
		//seitcm_destroy_database();
	}
}

/*
 * Uninstall SEITCM Plugin
 * */
function seitcm_uninstall_me() {
	if(current_user_can('delete_plugins'))
	{
		require_once SEITCM_INCLUDES_DIR.'seitcm_dbinit.php';
		seitcm_destroy_database();
		require_once SEITCM_INCLUDES_DIR.'seitcm_custom_roles.php';
		seitcm_remove_role();
	}
}

/*
 * 
 * */
function seitcm_login_redirect( $redirect_to, $request, $user ){
    if( isset( $user->roles ) && is_array( $user->roles ) ) {
        if( in_array( "administrator", $user->roles ) ) {
            return $redirect_to;
        } 
        if( in_array('seitcm_student', $user->roles)) {
        	return admin_url("admin.php?page=seitcm_main_option_menu");
        }
        if( in_array('seitcm_teacher', $user->roles)) {
        	return admin_url("admin.php?page=seitcm_main_option_menu");
        }
        if( in_array('seitcm_counselor', $user->roles)) {
        	return admin_url("admin.php?page=seitcm_main_option_menu");
        }
        if( in_array('seitcm_manager', $user->roles)) {
        	return admin_url("admin.php?page=seitcm_main_option_menu");
        }
        else {
            //return home_url();
        }
    }
    else {
        return $redirect_to;
    }
}
add_filter("login_redirect", "seitcm_login_redirect", 10, 3);
?>